<template>
  <view class="t-info">
    <view class="t-b">设置新密码</view>
    <form class="cl">
      <view class="t-a">
        <input type="password" name="newPassword" placeholder="请设置6-12位字符、包含数字和字母的密码" maxlength="12" v-model="newPassword"/>
      </view>
      <view class="t-a">
        <input type="password" name="rePassword" maxlength="12" placeholder="请确认密码" v-model="rePassword"/>
      </view>
      <button @tap="resetPassword()">确定</button>
    </form>
  </view>
</template>

<script>
import userApi from '@/api/user'
import passData from '@/api/requstUtil'
import {aesEncrypt, kk} from '@/common/js/crypto'
export default {
  data() {
    return {
      userData: {},
      newPassword: '',
      rePassword: '',
      showText: true,
      second: 60
    }
  },
  onLoad(option) {
    const eventChannel = this.getOpenerEventChannel();
    let that = this
    eventChannel.on('getUserData', function (data) {
      that.userData = data;
      console.log(data)
    })
  },
  methods: {
    resetPassword() {
      if (this.checkParam()) {
        let encrypt = aesEncrypt(this.userData.phone + this.newPassword, kk);
        let param = {
          phone: this.userData.phone,
          userName:this.userData.phone,
          password: encrypt,
          tenantId: 'gysk',
          smsToken: this.userData.smsToken,
          changeType:'SMS'
        }
        userApi.reset(param).then(res => {
          let data = passData(res);
          console.log("123444",data)
          if (data) {
            uni.showToast({title: '密码修改成功！', icon: 'none'});
            uni.navigateTo({url: '/pages/auth/login'});
          }
        })
      }

    },

    checkParam() {
      if (!this.newPassword) {
        uni.showToast({title: '请输入密码', icon: 'none'});
        return false;
      }
      if (!this.rePassword || this.newPassword !== this.rePassword) {
        uni.showToast({title: '密码必须一致', icon: 'none'});
        return false;
      }
      return true;

    }
  }
}
</script>

<style scoped>

.t-b {
  font-size: 18px;
  color: #000;
  padding: 35px 0 30px 0;
  font-weight: bold;
  text-align: center;
}


.t-info {
  width: 600rpx;
  margin: 0 auto;
  font-size: 28rpx;
  color: #000;
}

.t-info button {
  font-size: 14px;
  background: rgb(240, 95, 93);
  color: #fff;
  height: 42px;
  line-height: 45px;
  border-radius: 9px;
  box-shadow: 0 5px 7px 0 rgba(86, 119, 252, 0.2);
  width: 55%;
}

.t-info input {
  padding: 0 10px 0 21px;
  height: 46px;
  line-height: 46px;
  margin-bottom: 26px;
  background: #f8f7fc;
  border-bottom: 1px solid #e9e9e9;
  font-size: 15px;
  border-radius: 5px;
}

</style>
